﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Goldlight.Mvvm.Base.Command;

namespace Goldlight.Mvvm.Base
{
    /// <summary>
    /// Workspace message raised via the <see cref="Mediator"/>.
    /// </summary>
    public enum WorkspaceMessages
    {
        /// <summary>
        /// Message raised when the workspace is closed.
        /// </summary>
        RequestCloseWorkspace,
    }
    /// <summary>
    /// Base class for view models that want to enlist in view models.
    /// </summary>
    public abstract class WorkspaceViewModelBase : ViewModelBase
    {
        #region Members
        private RelayCommand _closeCommand;
        private Guid _instanceGuid;
        private bool _closeable = true;
        #endregion

        #region Public methods
        /// <summary>
        ///  Initializes a new instance of <see cref="WorkspaceViewModelBase"/>
        /// </summary>
        public WorkspaceViewModelBase() : base() {}

        /// <summary>
        /// The Id for this particular instance.
        /// </summary>
        public Guid InstanceId
        {
            get { return _instanceGuid; }
        }
        
        /// <summary>
        /// Can this workspace be closed?
        /// </summary>
        public bool IsCloseable
        {
            get
            {
                return _closeable;
            }
            set
            {
                if (_closeable != value)
                {
                    _closeable = value;
                    OnChanged("IsCloseable");
                }
            }
        }

        /// <summary>
        /// When a close is requested, this command notifies the application that
        /// this workspace should be removed from the UI.
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new RelayCommand(
                        () => Mediator.NotifyColleagues(WorkspaceMessages.RequestCloseWorkspace, this));
                }
                return _closeCommand;
            }
        }
        #endregion

        /// <summary>
        /// Initialize the instance.
        /// </summary>
        protected override void OnInitialize()
        {
            base.OnInitialize();
            _instanceGuid = Guid.NewGuid();
        }
    }
}